"""
通知模型
"""

from datetime import datetime
from enum import Enum
from app import db


class NotificationType(Enum):
    """通知类型枚举"""
    TASK_ASSIGNED = "task_assigned"
    TASK_COMPLETED = "task_completed"
    TASK_OVERDUE = "task_overdue"
    PROJECT_UPDATED = "project_updated"
    TEAM_INVITATION = "team_invitation"
    COMMENT_ADDED = "comment_added"
    SYSTEM = "system"


class Notification(db.Model):
    """通知模型"""
    __tablename__ = 'notifications'
    
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    type = db.Column(db.Enum(NotificationType), nullable=False)
    title = db.Column(db.String(200), nullable=False)
    message = db.Column(db.Text, nullable=False)
    
    # 关联数据
    related_id = db.Column(db.Integer, nullable=True)  # 关联的任务、项目等ID
    related_type = db.Column(db.String(50), nullable=True)  # 关联类型
    
    # 状态字段
    is_read = db.Column(db.Boolean, default=False, nullable=False)
    is_sent = db.Column(db.Boolean, default=False, nullable=False)
    
    # 时间字段
    created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
    read_at = db.Column(db.DateTime, nullable=True)
    sent_at = db.Column(db.DateTime, nullable=True)
    
    def __repr__(self):
        return f'<Notification {self.title}>'
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'user_id': self.user_id,
            'type': self.type.value,
            'title': self.title,
            'message': self.message,
            'related_id': self.related_id,
            'related_type': self.related_type,
            'is_read': self.is_read,
            'is_sent': self.is_sent,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'read_at': self.read_at.isoformat() if self.read_at else None,
            'sent_at': self.sent_at.isoformat() if self.sent_at else None
        }
    
    def mark_as_read(self):
        """标记为已读"""
        self.is_read = True
        self.read_at = datetime.utcnow()
        db.session.commit()
    
    def mark_as_sent(self):
        """标记为已发送"""
        self.is_sent = True
        self.sent_at = datetime.utcnow()
        db.session.commit()
    
    @classmethod
    def create_notification(cls, user_id, notification_type, title, message, related_id=None, related_type=None):
        """创建通知"""
        notification = cls(
            user_id=user_id,
            type=notification_type,
            title=title,
            message=message,
            related_id=related_id,
            related_type=related_type
        )
        db.session.add(notification)
        db.session.commit()
        return notification
    
    @classmethod
    def get_user_notifications(cls, user_id, unread_only=False):
        """获取用户通知"""
        query = cls.query.filter_by(user_id=user_id)
        if unread_only:
            query = query.filter_by(is_read=False)
        return query.order_by(cls.created_at.desc()).all()
    
    @classmethod
    def get_unread_count(cls, user_id):
        """获取未读通知数量"""
        return cls.query.filter_by(user_id=user_id, is_read=False).count()
    
    @classmethod
    def mark_all_as_read(cls, user_id):
        """标记所有通知为已读"""
        cls.query.filter_by(user_id=user_id, is_read=False).update({
            'is_read': True,
            'read_at': datetime.utcnow()
        })
        db.session.commit()
